package adapters;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.nostra13.universalimageloader.core.ImageLoader;

import utils.Falha;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.ufba.square.registro_square.R;

public class CustomAdapterFalhas extends BaseAdapter {
	
	private Context context;
	private ArrayList<Falha> falhas;

	public CustomAdapterFalhas(Context context, ArrayList<Falha> falhas){
		this.context = context;
		this.falhas = falhas;
	}

	@Override
	public int getCount() {
		return falhas.size();
	}

	@Override
	public Object getItem(int position) {		
		return falhas.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Falha> getItems(){
		return falhas;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JSONObject json = new JSONObject();

		//if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater)
				context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

			convertView = mInflater.inflate(R.layout.activity_main_listview_item, null);
		//}

		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.image_falha);
		TextView txtLocal = (TextView) convertView.findViewById(R.id.fld_local);
		TextView txtdataregistro = (TextView) convertView.findViewById(R.id.fld_dataregistro);
		TextView txtLocalInterno = (TextView) convertView.findViewById(R.id.fld_localinterno);
		TextView txtCategoria = (TextView) convertView.findViewById(R.id.fld_categoria);
		TextView txtStatus = (TextView) convertView.findViewById(R.id.fld_status);

		ImageLoader.getInstance().displayImage(falhas.get(position).foto,imgIcon);

		txtLocal.setText(falhas.get(position).local);//falhas.get(position).getArena()
		txtdataregistro.setText(falhas.get(position).data_ocorrencia);
		txtLocalInterno.setText(falhas.get(position).localinterno);
		txtCategoria.setText(falhas.get(position).categoria);
		if (falhas.get(position).status=="FALHA POSTADA")
			txtStatus.setTextColor(Color.RED);
		txtStatus.setText(falhas.get(position).status);

		//if (falhas.get(position).!=0)
		//	imgIcon.setImageResource(falhas.get(position).getIcon());
		//else
			//imgIcon.setVisibility(View.GONE);

		//if (falhas.get(position).getFoto()!="")
			//txtStatus.setText(falhas.get(position).status);
		//else
			//txtStatus.setVisibility(View.GONE);
		//System.out.println(navDrawerItems.get(position));

		try{
			//System.out.println(navDrawerItems.get(position).getComando());
			//json = (JSONObject) new JSONTokener(falhas.get(position).getFoto()).nextValue();
			json = (JSONObject) new JSONTokener("").nextValue();
			txtLocal.setTag(json);
		}catch (JSONException e) {
			//System.out.println("Erro ");
		}

		//if (falhas.get(position).getFoto()){
			//RelativeLayout layout = (RelativeLayout) convertView.findViewById(R.id.relativeitem);
			//layout.setBackgroundColor(Color.parseColor("#75a00e"));
		//	MarginLayoutParams params = (MarginLayoutParams) imgIcon.getLayoutParams();
		//	params.leftMargin = 48;
		//	imgIcon.setLayoutParams(params);
		//}

		return convertView;
	}

}